﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using ProductKeyManager.DB;
using ProductKeyManager.UI;

namespace ProductKeyManager
{
    /// <summary>
    /// Interaction logic for NewDialog.xaml
    /// </summary>
    public partial class ProductDialog : Window
    {
        public ProductDialog()
        {
            InitializeComponent();
        }

        private DialogMode _Mode = DialogMode.NewProduct;

        public DialogMode Mode
        {
            get { return this._Mode; }
            set { this._Mode = value; }
        }

        private Product _NewProduct = new Product {  };

        public Product NewProduct
        {
            get 
            {
                foreach (ProductKeyManager.DB.Key code in _KeyCodes)
                {
                    _NewProduct.Keys.Add(code);
                }

                return _NewProduct;
            }
        }

        private Product _OldKey;

        public Product OldKey
        {
            get { return _OldKey; }
            set
            {
                _OldKey = value;
                _NewProduct = new Product { Name = _OldKey.Name};
                foreach (ProductKeyManager.DB.Key code in _OldKey.Keys)
                {
                    _KeyCodes.Add(new ProductKeyManager.DB.Key { KeyCode = code.KeyCode, AllocatedTo = code.AllocatedTo });
                }
            }
        }

        private ObservableCollection<ProductKeyManager.DB.Key> _KeyCodes = new ObservableCollection<ProductKeyManager.DB.Key>();

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            switch (this._Mode)
            {
                case DialogMode.EditProduct:
                    this.Title = this.LoadStringResource("TitleEdit");
                    break;
                case DialogMode.ViewProduct:
                    this.Title = this.LoadStringResource("TitleView");
                    this.txtProductName.IsReadOnly = true;
                    this.AddEditToolbar.Visibility = Visibility.Collapsed;
                    break;
                case DialogMode.NewProduct:
                default:
                    this.Title = this.LoadStringResource("TitleNew");
                    break;
            }
            this.lblProductName.Content = string.Format("{0}:", this.LoadStringResource("ProductName"));
            this.btnAddKeyCode.ToolTip = this.LoadStringResource("AddKeyCodeToolTip");
            this.btnRemoveKeyCode.ToolTip = this.LoadStringResource("RemoveKeyCodeToolTip");

            this.txtProductName.Width = ((StackPanel)this.txtProductName.Parent).ActualWidth;

            this.DataContext = _NewProduct;
            lstProdKeys.DataContext = _KeyCodes;
            AddEditToolbar.DataContext = _KeyCodes;
        }

        private void AddKeyCode(object sender, RoutedEventArgs e)
        {
            ProductKeyManager.DB.Key code = new ProductKeyManager.DB.Key { KeyCode = this.LoadStringResource("NewKey"), AllocatedTo = this.LoadStringResource("NotAllocated") };
            _KeyCodes.Add(code);
            lstProdKeys.SelectedItem = code;
        }

        private void RemoveKeyCode(object sender, RoutedEventArgs e)
        {
            _KeyCodes.Remove((ProductKeyManager.DB.Key)lstProdKeys.SelectedItem);
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.txtProductName.Width = ((StackPanel)this.txtProductName.Parent).ActualWidth;
        }
    }
}
